<?php
require_once dirname(__FILE__) . '/../config/config.php';
class GameController
{
    private $smarty;
    public function __construct()
    {
    }

    public function main(&$smarty)
    {
        $this->smarty = $smarty;
    }

    public function listAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $seo = new SEO();
        $gameCategoryService = new GameCategoryService();

        $pager = new Pager();

        $curpage = isset ($_GET['page']) ? $_GET['page'] : 1;

        if(isset($_GET['database']) && !empty($_GET['database']))
        {
            $data = $_GET['database'];
            if($data == 'tpg')
            {
            	$gameCategoryArr = $gameCategoryService->getFullGameCategoryTPG($context, $result);
            	if(isset($_POST) && !empty($_POST))
            	{
            		$lastStr = "controller=game&action=list&database=tpg&filter=filter";
            		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filter')
            		{
            			$filter = $_POST['filter'];
            			if($filter == '5')
            			{
            				$count = $gameService->countGameByHotStarTPG($context, $result, $filter);
            				$limit = 10000;
            				$start = $pager->findStart($limit);
            				$pages = $pager->findPages($count, $limit);
            				$gameArr = $gameService->getGameByHotStarTPG($context, $result,$filter, $start, $limit);
            			}
            			else if($filter == '1')
            			{
            				$count = $gameService->countGameByEventTPG($context, $result, $filter);
            				$limit = 10000;
            				$start = $pager->findStart($limit);
            				$pages = $pager->findPages($count, $limit);
            				$gameArr = $gameService->getGameEventTPG($context, $result,$filter, $start, $limit);
            			}
            			$this->smarty->assign('filter', $filter);
            		}
            		else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterParent')
                    {
                    	$filter = $_POST['filter'];
                        $count = $gameService->countGameByParentTPG($context, $result, $filter);
                        $limit = 10000;
                        $start = $pager->findStart($limit);
                        $pages = $pager->findPages($count, $limit);
                        $gameArr = $gameService->getGameByParentTPG($context, $result,$filter, $start, $limit);
                        $this->smarty->assign('filter', $filter);
                    }
            		else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'type')
                    {
                    	$filterP = $_POST['filterP'];
                    	$filterType = $_POST['filterType'];
                        $gameArr = $gameService->getGameByParentAndTypeTPG($context, $result,$filterP, $filterType);
                    	$lastStr = "controller=game&action=list&database=tpg&filter=type";
                    	$this->smarty->assign('filterP', $filterP);
                   		$this->smarty->assign('filterType', $filterType);
                    }
                    
                }
                else
                {
                    $count = $gameService->countFullGameTPG($context, $result);
                    $limit = 30;
                    $start = $pager->findStart($limit);
                    $pages = $pager->findPages($count, $limit);
                    $gameArr = $gameService->getFullGameByLimitTPG($context, $result, $start, $limit);
                    $lastStr = "controller=game&action=list&database=tpg";
                }
                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            }
            else if($data == 'cgmb')
            {
                $gameCategoryArr = $gameCategoryService->getFullGameCategoryCGMB($context, $result);
                if(isset($_POST) && !empty($_POST))
                {
                    $lastStr = "controller=game&action=list&database=cgmb&filter=filter";
                    if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filter')
                    {
                    	$filter = $_POST['filter'];
                        if($filter == '5')
                        {
                            $count = $gameService->countGameByHotStarCGMB($context, $result, $filter);
                            $limit = 10000;
                            $start = $pager->findStart($limit);
                            $pages = $pager->findPages($count, $limit);
                            $gameArr = $gameService->getGameByHotStarCGMB($context, $result,$filter, $start, $limit);
                        }
                        else if($filter == '1')
                        {
                            $count = $gameService->countGameByEventCGMB($context, $result, $filter);
                            $limit = 10000;
                            $start = $pager->findStart($limit);
                            $pages = $pager->findPages($count, $limit);
                            $gameArr = $gameService->getGameEventCGMB($context, $result,$filter, $start, $limit);
                        }
                        $this->smarty->assign('filter', $filter);
                    }
                    else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterParent')
                    {
                    	$filter = $_POST['filter'];
                        $count = $gameService->countGameByParentCGMB($context, $result, $filter);
                        $limit = 10000;
                        $start = $pager->findStart($limit);
                        $pages = $pager->findPages($count, $limit);
                        $gameArr = $gameService->getGameByParentCGMB($context, $result,$filter, $start, $limit);
                        $this->smarty->assign('filter', $filter);
                    }
                	else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'type')
                    {
                    	$filterP = $_POST['filterP'];
                    	$filterType = $_POST['filterType'];
                        $gameArr = $gameService->getGameByParentAndTypeCGMB($context, $result,$filterP, $filterType);
                    	$lastStr = "controller=game&action=list&database=cgmb&filter=type";
                    	$this->smarty->assign('filterP', $filterP);
                   		$this->smarty->assign('filterType', $filterType);
                    }
                }
                else
                {
                    $count = $gameService->countFullGameCGMB($context, $result);
                    $limit = 30;
                    $start = $pager->findStart($limit);
                    $pages = $pager->findPages($count, $limit);
                    $gameArr = $gameService->getFullGameByLimitCGMB($context, $result, $start, $limit);
                    $lastStr = "controller=game&action=list&database=cgmb";
                }
                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            }
            else if($data == 'nsgt')
            {
                $gameCategoryArr = $gameCategoryService->getFullGameCategoryNSGT($context, $result);
                if(isset($_POST) && !empty($_POST))
                {
                    $lastStr = "controller=game&action=list&database=nsgt&filter=filter";
                    if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filter')
                    {
                    	$filter = $_POST['filter'];
                        if($filter == '5')
                        {
                            $count = $gameService->countGameByHotStarNSGT($context, $result, $filter);
                            $limit = 10000;
                            $start = $pager->findStart($limit);
                            $pages = $pager->findPages($count, $limit);
                            $gameArr = $gameService->getGameByHotStarNSGT($context, $result,$filter, $start, $limit);
                        }
                        else if($filter == '1')
                        {
                            $count = $gameService->countGameByEventNSGT($context, $result, $filter);
                            $limit = 10000;
                            $start = $pager->findStart($limit);
                            $pages = $pager->findPages($count, $limit);
                            $gameArr = $gameService->getGameEventNSGT($context, $result,$filter, $start, $limit);
                        }
                        $this->smarty->assign('filter', $filter);
                    }
                    else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterParent')
                    {
                    	$filter = $_POST['filter'];
                        $count = $gameService->countGameByParentNSGT($context, $result, $filter);
                        $limit = 10000;
                        $start = $pager->findStart($limit);
                        $pages = $pager->findPages($count, $limit);
                        $gameArr = $gameService->getGameByParentNSGT($context, $result,$filter, $start, $limit);
                        $this->smarty->assign('filter', $filter);
                    }
               	 	else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'type')
                    {
                    	$filterP = $_POST['filterP'];
                    	$filterType = $_POST['filterType'];
                        $gameArr = $gameService->getGameByParentAndTypeNSGT($context, $result,$filterP, $filterType);
                    	$lastStr = "controller=game&action=list&database=nsgt&filter=type";
                    	$this->smarty->assign('filterP', $filterP);
                   		$this->smarty->assign('filterType', $filterType);
                    }
                }
                else
                {
                    $count = $gameService->countFullGameNSGT($context, $result);
                    $limit = 30;
                    $start = $pager->findStart($limit);
                    $pages = $pager->findPages($count, $limit);
                    $gameArr = $gameService->getFullGameByLimitNSGT($context, $result, $start, $limit);
                    $lastStr = "controller=game&action=list&&database=nsgt";
                }
                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            }
            else if($data == 'mmbg')
            {
                $gameCategoryArr = $gameCategoryService->getFullGameCategoryMMBG($context, $result);
                if(isset($_POST) && !empty($_POST))
                {
                    $lastStr = "controller=game&action=list&database=mmbg&filter=filter";
                    if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filter')
                    {
                    	$filter = $_POST['filter'];
                        if($filter == '5')
                        {
                            $count = $gameService->countGameByHotStarMMBG($context, $result, $filter);
                            $limit = 10000;
                            $start = $pager->findStart($limit);
                            $pages = $pager->findPages($count, $limit);
                            $gameArr = $gameService->getGameByHotStarMMBG($context, $result,$filter, $start, $limit);
                        }
                        else if($filter == '1')
                        {
                            $count = $gameService->countGameByEventMMBG($context, $result, $filter);
                            $limit = 10000;
                            $start = $pager->findStart($limit);
                            $pages = $pager->findPages($count, $limit);
                            $gameArr = $gameService->getGameEventMMBG($context, $result,$filter, $start, $limit);
                        }
                        $this->smarty->assign('filter', $filter);
                    }
                    else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterParent')
                    {
                    	$filter = $_POST['filter'];
                        $count = $gameService->countGameByParentMMBG($context, $result, $filter);
                        $limit = 10000;
                        $start = $pager->findStart($limit);
                        $pages = $pager->findPages($count, $limit);
                        $gameArr = $gameService->getGameByParentMMBG($context, $result,$filter, $start, $limit);
                        $this->smarty->assign('filter', $filter);
                    }
                	else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'type')
                    {
                    	$filterP = $_POST['filterP'];
                    	$filterType = $_POST['filterType'];
                        $gameArr = $gameService->getGameByParentAndTypeMobile($context, $result,$filterP, $filterType);
                    	$lastStr = "controller=game&action=list&database=mmbg&filter=type";
                    	$this->smarty->assign('filterP', $filterP);
                   		$this->smarty->assign('filterType', $filterType);
                    }
                }
                else
                {
                    $count = $gameService->countFullGameMMBG($context, $result);
                    $limit = 30;
                    $start = $pager->findStart($limit);
                    $pages = $pager->findPages($count, $limit);
                    $gameArr = $gameService->getFullGameByLimitMMBG($context, $result, $start, $limit);
                    $lastStr = "controller=game&action=list&database=mmbg";
                }
                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            }
        	else if($data == 'mobile')
            {
                $gameCategoryArr = $gameCategoryService->getFullGameCategoryMobile($context, $result);
                if(isset($_POST) && !empty($_POST))
                {
                    $lastStr = "controller=game&action=list&database=tpg&filter=filter";
                    if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filter')
                    {
                    	$filter = $_POST['filter'];
                        if($filter == '5')
                        {
                            $count = $gameService->countGameByHotStarMobile($context, $result, $filter);
                            $limit = 10000;
                            $start = $pager->findStart($limit);
                            $pages = $pager->findPages($count, $limit);
                            $gameArr = $gameService->getGameByHotStarMobile($context, $result,$filter, $start, $limit);
                        }
                        else if($filter == '1')
                        {
                            $count = $gameService->countGameByEventMobile($context, $result, $filter);
                            $limit = 10000;
                            $start = $pager->findStart($limit);
                            $pages = $pager->findPages($count, $limit);
                            $gameArr = $gameService->getGameEventMobile($context, $result,$filter, $start, $limit);
                        }
                        $this->smarty->assign('filter', $filter);
                    }
                    else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterParent')
                    {
                    	$filter = $_POST['filter'];
                        $count = $gameService->countGameByParentMobile($context, $result, $filter);
                        $limit = 10000;
                        $start = $pager->findStart($limit);
                        $pages = $pager->findPages($count, $limit);
                        $gameArr = $gameService->getGameByParentMobile($context, $result,$filter, $start, $limit);
                    	$this->smarty->assign('filter', $filter);
                    }
                	else if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'type')
                    {
                    	$filterP = $_POST['filterP'];
                    	$filterType = $_POST['filterType'];
                        $gameArr = $gameService->getGameByParentAndTypeMobile($context, $result,$filterP, $filterType);
                    	$lastStr = "controller=game&action=list&database=mobile&filter=type";
                    	$this->smarty->assign('filterP', $filterP);
                   		$this->smarty->assign('filterType', $filterType);
                    }
                }
                else
                {
                    $count = $gameService->countFullGameMobile($context, $result);
                    $limit = 30;
                    $start = $pager->findStart($limit);
                    $pages = $pager->findPages($count, $limit);
                    $gameArr = $gameService->getFullGameByLimitMobile($context, $result, $start, $limit);
                    $lastStr = "controller=game&action=list&database=mobile";
                }
                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            }
            $this->smarty->assign('data', $data);
        }
        else
        {
            $gameCategoryArr = $gameCategoryService->getFullGameCategoryTPG($context, $result);
            $count = $gameService->countFullGameTPG($context, $result);
            $limit = 30;
            $start = $pager->findStart($limit);
            $pages = $pager->findPages($count, $limit);
            $gameArr = $gameService->getFullGameByLimitTPG($context, $result, $start, $limit);
            $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            $lastStr = "controller=game&action=list&database=tpg";
        }
        if(isset($pager) && isset($pages) && isset($lastStr))
        {
	        $pageFirst = $pager->findFirst($curpage, $pages, $lastStr);
	        $pagePrev = $pager->findPrev($curpage, $pages, $lastStr);
	        $pageList = $pager->pageList($curpage, $pages, $lastStr);
	        $pageNext = $pager->findNext($curpage, $pages, $lastStr);
	        $pageLast = $pager->findLast($curpage, $pages, $lastStr);
	
	        $this->smarty->assign('pageFirst', $pageFirst);
	        $this->smarty->assign('pagePrev', $pagePrev);
	        $this->smarty->assign('pageList', $pageList);
	        $this->smarty->assign('pageNext', $pageNext);
	        $this->smarty->assign('pageLast', $pageLast);
	    }
        if (isset($count))
        {
	        
			$this->smarty->assign('count', $count);
	        
        }
        $this->smarty->assign('gameArr', $gameArr);
        $this->smarty->display('list-game.tpl');
    }

    public function addAction()
   	{
    	$_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $gameCategoryService  = new GameCategoryService();

        $gameTagArr = $gameService->getFullGameTag($context, $result);
        $gameCategoryArr = $gameCategoryService->getFullGameCategory($context, $result);
		$gamekinhdienArr = $gameCategoryService -> getGameCategoryByParentMobile($context, $result, 78);     
		
        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
        $this->smarty->assign('gameTagArr', $gameTagArr);
        $this->smarty->assign('gamekinhdienArr',$gamekinhdienArr);
        $this->smarty->display('add-game.tpl');   
    }
    
	public function addMMBGAction()
   	{
    	$_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $gameCategoryService  = new GameCategoryService();

        $gameTagArr = $gameService->getFullGameTag($context, $result);
        $gameCategoryArr = $gameCategoryService->getFullGameCategoryMMBG($context, $result);
		$gamekinhdienArr = $gameCategoryService -> getGameCategoryByParentMMBG1($context, $result, 78);     
		
        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
        $this->smarty->assign('gameTagArr', $gameTagArr);
        $this->smarty->assign('gamekinhdienArr',$gamekinhdienArr);
        $this->smarty->display('add-game-mymobigame.tpl');   
    }
    
	public function addMobileAction()
   	{
    	$_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $gameCategoryService  = new GameCategoryService();

        $gameTagArr = $gameService->getFullGameTagMobile($context, $result);
        $gameCategoryArr = $gameCategoryService->getFullGameCategoryMobile($context, $result);
		$gamekinhdienArr = $gameCategoryService -> getGameCategoryByParentMobile($context, $result, 78);
		
        if(isset($_POST) && !empty($_POST))
        {
        	if ($_POST["loaigame"] == 3)
            {
                $folder = 'offline';
            }
            else if($_POST["loaigame"] == 78)
            {
            	 $folder = 'kinhdien';
            }
            else if($_POST["loaigame"] == 2)
            {
               $folder = 'online';
            }
        	else if($_POST["loaigame"] == 4)
            {
               $folder = 'webgame';
            }
		            
            if($_POST["kieugame"])
			{
				$kieugame = SEO::friendly_url_image($_POST["kieugame"]);
				$_POST['smallImage'] = 'games'.'/'. $folder .'/'. $kieugame.'/mobile'.'/'.$_POST['image'];
		        $_POST['mediumImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/small'.'/'.$_POST['image'];
		        $_POST['largeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/large'.'/'.$_POST['image'];
		        $_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/xlarge'.'/'.$_POST['xlargeImage'];
			}
			else 
			{
				$_POST['smallImage'] = 'games'.'/'. $folder .'/'.'mobile'.'/'.$_POST['image'];
                $_POST['mediumImage'] = 'games'.'/'. $folder .'/'.'small'.'/'.$_POST['image'];
                $_POST['largeImage'] = 'games'.'/'. $folder .'/'.'large'.'/'.$_POST['image'];
                $_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.'xlarge'.'/'.$_POST['xlargeImage'];
			}

            $value = $_POST['tag1'];
            $_POST['tag'] ='';
            if ($value){
                foreach ($value as $v)
                {
                	$tagname .= $v.',';
                    $_POST['tag'] = substr($tagname,0,-1);
                }
            }
            $_POST['longDetail'] = stripslashes($_POST['longDetail']);
            $_POST['shortDetail'] = stripslashes($_POST['shortDetail']);
            $_POST['mediumDetail'] = stripslashes($_POST['mediumDetail']);
            $_POST['mediumName'] = stripslashes($_POST['mediumName']);
            $_POST['unique_name'] = SEO::friendly_url($_POST['mediumName']);
            $param = $_POST;
            $gameService->insertGameMobile($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=mobile");
        }
        else
        {
            $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            $this->smarty->assign('gameTagArr', $gameTagArr);
            $this->smarty->assign('gamekinhdienArr',$gamekinhdienArr);
            $this->smarty->display('add-game-mobile.tpl');
        }
    }

    public function updateAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $gameCategoryService  = new GameCategoryService();

        if(isset($_GET['database']) && !empty($_GET['database']))
        {
            $data = $_GET['database'];

            if($data == 'tpg')
            {
            	$gamekinhdienArr = $gameCategoryService -> getGameCategoryByParentMobile($context, $result, 78);
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $game = $gameService->getGameByIDTPG($context, $result, $id);
                $gameTagArr = $gameService->getFullGameTagTPG($context, $result);
                $gameCategoryArr = $gameCategoryService->getFullGameCategoryTPG($context, $result);

                $image = explode('/',$game['mediumImage']);
                
            	$kieugameKD = $image[2];
			 	if($image[1] == 'kinhdien')
		        {
		        	$img = $image[4];
		        }
		        else
		        {
		        	$img = $image[3];
		        }
            	$imagex = explode('/',$game['xlargeImage']);
				if($imagex[1] == 'kinhdien')
		        {
		        	$imgx = $image[4];
		        }
		        else
		        {
		        	$imgx = $image[3];
		        }
		        
                $loaiGame = $image[1];

                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
                $this->smarty->assign('loaiGame', $loaiGame);
                $this->smarty->assign('img', $img);
                $this->smarty->assign('imgx', $imgx);
                $this->smarty->assign('game', $game);
                $this->smarty->assign('kieugameKD', $kieugameKD);
                $tag = $game['tag'];
                $mang = explode(',',$tag);
                $this->smarty->assign('mang', $mang);
                if(isset($_POST) && !empty($_POST))
                {
                	if ($_POST["loaigame"] == 3)
		            {
		                $folder = 'offline';
		            }
		            else if($_POST["loaigame"] == 78)
		            {
		            	 $folder = 'kinhdien';
		            }
		            else if($_POST["loaigame"] == 2 || $_POST["loaigame"] == 156)
		            {
		               $folder = 'online';
		            }
		        	else if($_POST["loaigame"] == 4)
		            {
		               $folder = 'webgame';
		            }
		            
                	if($_POST["kieugame"])
					{
						$kieugame = SEO::friendly_url_image($_POST["kieugame"]);
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'. $kieugame.'/thumbnail'.'/'.$_POST['image'];
		           		$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/small'.'/'.$_POST['image'];
		            	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/large'.'/'.$_POST['image'];
		            	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/xlarge'.'/'.$_POST['xlargeImage'];
					}
					else 
					{
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'.'thumbnail'.'/'.$_POST['image'];
                    	$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.'small'.'/'.$_POST['image'];
                    	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.'large'.'/'.$_POST['image'];
                    	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.'xlarge'.'/'.$_POST['xlargeImage'];
					}

                    $value = $_POST['tag1'];
                    $_POST['tag'] ='';
                    if ($value){
                        foreach ($value as $v)
                        {
                           	$tagname .= $v.',';
                    		$_POST['tag'] = substr($tagname,0,-1);
                        }
                    }

                    $_POST['id'] = $_POST['id'];
                    $_POST['shortDetail'] = stripslashes($_POST['shortDetail']);
            		$_POST['mediumDetail'] = stripslashes($_POST['mediumDetail']);
                    $_POST['longDetail'] = stripslashes($_POST['longDetail']);
                    $_POST['name'] = stripslashes($_POST['name']);
                    $_POST['mediumName'] = stripslashes($_POST['mediumName']);

                    $param = $_POST;
                    $gameService->updateGameTPG($context, $result, $param);

                    header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=tpg");
                }
                $this->smarty->assign('data',$data);
                $this->smarty->assign('gamekinhdienArr',$gamekinhdienArr);
                $this->smarty->assign('gameTagArr', $gameTagArr);
                $this->smarty->display('update-game.tpl');
            }
            elseif($data == 'mobile')
            {
            	$gamekinhdienArr = $gameCategoryService -> getGameCategoryByParentMobile($context, $result, 78);
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $game = $gameService->getGameByIDMobile($context, $result, $id);
                $gameTagArr = $gameService->getFullGameTagMobile($context, $result);
                $gameCategoryArr = $gameCategoryService->getFullGameCategoryMobile($context, $result);

                $image = explode('/',$game['mediumImage']);
                
            	$kieugameKD = $image[2];
			 	if($image[1] == 'kinhdien')
		        {
		        	$img = $image[4];
		        }
		        else
		        {
		        	$img = $image[3];
		        }
            	$imagex = explode('/',$game['xlargeImage']);
				if($imagex[1] == 'kinhdien')
		        {
		        	$imgx = $image[4];
		        }
		        else
		        {
		        	$imgx = $image[3];
		        }
		        
                $loaiGame = $image[1];

                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
                $this->smarty->assign('loaiGame', $loaiGame);
                $this->smarty->assign('img', $img);
                $this->smarty->assign('imgx', $imgx);
                $this->smarty->assign('game', $game);
                $this->smarty->assign('kieugameKD', $kieugameKD);
                $tag = $game['tag'];
                $mang = explode(',',$tag);

                $this->smarty->assign('mang', $mang);
                if(isset($_POST) && !empty($_POST))
                {
                	if ($_POST["loaigame"] == 3)
		            {
		                $folder = 'offline';
		            }
		            else if($_POST["loaigame"] == 78)
		            {
		            	 $folder = 'kinhdien';
		            }
		            else if($_POST["loaigame"] == 2 || $_POST["loaigame"] == 156)
		            {
		               $folder = 'online';
		            }
		        	else if($_POST["loaigame"] == 4)
		            {
		               $folder = 'webgame';
		            }
		            
                	if($_POST["kieugame"])
					{
						$kieugame = SEO::friendly_url_image($_POST["kieugame"]);
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'. $kieugame.'/mobile'.'/'.$_POST['image'];
		           		$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/small'.'/'.$_POST['image'];
		            	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/large'.'/'.$_POST['image'];
		            	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/xlarge'.'/'.$_POST['xlargeImage'];
					}
					else 
					{
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'.'mobile'.'/'.$_POST['image'];
                    	$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.'small'.'/'.$_POST['image'];
                    	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.'large'.'/'.$_POST['image'];
                    	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.'xlarge'.'/'.$_POST['xlargeImage'];
					}

                    $value = $_POST['tag1'];
                    $_POST['tag'] ='';
                    if ($value){
                        foreach ($value as $v)
                        {
                           	$tagname .= $v.',';
                    		$_POST['tag'] = substr($tagname,0,-1);
                        }
                    }

                    $_POST['id'] = $_POST['id'];
                    $_POST['shortDetail'] = stripslashes($_POST['shortDetail']);
            		$_POST['mediumDetail'] = stripslashes($_POST['mediumDetail']);
                    $_POST['longDetail'] = stripslashes($_POST['longDetail']);
                    $_POST['name'] = stripslashes($_POST['name']);
                    $_POST['mediumName'] = stripslashes($_POST['mediumName']);

                    $param = $_POST;
                    $gameService->updateGameMobile($context, $result, $param);
                    header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=mobile");
                }
                $this->smarty->assign('data',$data);
                $this->smarty->assign('gameTagArr', $gameTagArr);
                $this->smarty->assign('gamekinhdienArr',$gamekinhdienArr);
                $this->smarty->display('update-game.tpl');
            }
            elseif ($data == 'cgmb')
            {
            	$gamekinhdienArr = $gameCategoryService -> getGameCategoryByParentMobile($context, $result, 78);
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $game = $gameService->getGameByIDCGMB($context, $result, $id);
                $gameTagArr = $gameService->getFullGameTagCGMB($context, $result);
                $gameCategoryArr = $gameCategoryService->getFullGameCategoryCGMB($context, $result);

                $image = explode('/',$game['mediumImage']);
                
            	$kieugameKD = $image[2];
			 	if($image[1] == 'kinhdien')
		        {
		        	$img = $image[4];
		        }
		        else
		        {
		        	$img = $image[3];
		        }
            	$imagex = explode('/',$game['xlargeImage']);
				if($imagex[1] == 'kinhdien')
		        {
		        	$imgx = $image[4];
		        }
		        else
		        {
		        	$imgx = $image[3];
		        }
		        
                $loaiGame = $image[1];

                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
                $this->smarty->assign('loaiGame', $loaiGame);
                $this->smarty->assign('img', $img);
                $this->smarty->assign('imgx', $imgx);
                $this->smarty->assign('game', $game);
                $this->smarty->assign('kieugameKD', $kieugameKD);
                $tag = $game['tag'];
                $mang = explode(',',$tag);

                $this->smarty->assign('mang', $mang);
                if(isset($_POST) && !empty($_POST))
                {
                	if ($_POST["loaigame"] == 3)
		            {
		                $folder = 'offline';
		            }
		            else if($_POST["loaigame"] == 78)
		            {
		            	 $folder = 'kinhdien';
		            }
		            else if($_POST["loaigame"] == 2 || $_POST["loaigame"] == 156)
		            {
		               $folder = 'online';
		            }
		        	else if($_POST["loaigame"] == 4)
		            {
		               $folder = 'webgame';
		            }
		            
                	if($_POST["kieugame"])
					{
						$kieugame = SEO::friendly_url_image($_POST["kieugame"]);
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'. $kieugame.'/thumbnail'.'/'.$_POST['image'];
		           		$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/small'.'/'.$_POST['image'];
		            	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/large'.'/'.$_POST['image'];
		            	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/xlarge'.'/'.$_POST['xlargeImage'];
					}
					else 
					{
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'.'thumbnail'.'/'.$_POST['image'];
                    	$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.'small'.'/'.$_POST['image'];
                    	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.'large'.'/'.$_POST['image'];
                    	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.'xlarge'.'/'.$_POST['xlargeImage'];
					}
					
                    $value = $_POST['tag1'];
                    $_POST['tag'] ='';
                    if ($value){
                        foreach ($value as $v)
                        {
                            $tagname .= $v.',';
                    		$_POST['tag'] = substr($tagname,0,-1);
                        }
                    }

                    $_POST['id'] = $_POST['id'];
                    $_POST['shortDetail'] = stripslashes($_POST['shortDetail']);
           			$_POST['mediumDetail'] = stripslashes($_POST['mediumDetail']);
                    $_POST['longDetail'] = stripslashes($_POST['longDetail']);
                    $_POST['name'] = stripslashes($_POST['name']);
                    $_POST['mediumName'] = stripslashes($_POST['mediumName']);

                    $param = $_POST;
                    $gameService->updateGameCGMB($context, $result, $param);
                    header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=cgmb");
                }
                $this->smarty->assign('data',$data);
                $this->smarty->assign('gameTagArr', $gameTagArr);
                $this->smarty->assign('gamekinhdienArr',$gamekinhdienArr);
                $this->smarty->display('update-game.tpl');
            }
            elseif ($data == 'mmbg')
            {
            	$gamekinhdienArr = $gameCategoryService -> getGameCategoryByParentMobile($context, $result, 78);
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $game = $gameService->getGameByIDMMBG($context, $result, $id);
                $gameTagArr = $gameService->getFullGameTagMMBG($context, $result);
                $gameCategoryArr = $gameCategoryService->getFullGameCategoryMMBG($context, $result);

                $image = explode('/',$game['mediumImage']);
                
            	$kieugameKD = $image[2];
			 	if($image[1] == 'kinhdien')
		        {
		        	$img = $image[4];
		        }
		        else
		        {
		        	$img = $image[3];
		        }
            	$imagex = explode('/',$game['xlargeImage']);
				if($imagex[1] == 'kinhdien')
		        {
		        	$imgx = $image[4];
		        }
		        else
		        {
		        	$imgx = $image[3];
		        }
		        
                $loaiGame = $image[1];

                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
                $this->smarty->assign('loaiGame', $loaiGame);
                $this->smarty->assign('img', $img);
                $this->smarty->assign('imgx', $imgx);
                $this->smarty->assign('game', $game);
                $this->smarty->assign('kieugameKD', $kieugameKD);
                $tag = $game['tag'];
                $mang = explode(',',$tag);

                $this->smarty->assign('mang', $mang);
                if(isset($_POST) && !empty($_POST))
                {
                	if ($_POST["loaigame"] == 3)
		            {
		                $folder = 'offline';
		            }
		            else if($_POST["loaigame"] == 78)
		            {
		            	 $folder = 'kinhdien';
		            }
		            else if($_POST["loaigame"] == 2 || $_POST["loaigame"] == 156)
		            {
		               $folder = 'online';
		            }
		        	else if($_POST["loaigame"] == 4)
		            {
		               $folder = 'webgame';
		            }
		            
                	if($_POST["kieugame"])
					{
						$kieugame = SEO::friendly_url_image($_POST["kieugame"]);
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'. $kieugame.'/thumbnail'.'/'.$_POST['image'];
		           		$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/small'.'/'.$_POST['image'];
		            	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/large'.'/'.$_POST['image'];
		            	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/xlarge'.'/'.$_POST['xlargeImage'];
					}
					else 
					{
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'.'thumbnail'.'/'.$_POST['image'];
                    	$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.'small'.'/'.$_POST['image'];
                    	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.'large'.'/'.$_POST['image'];
                    	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.'xlarge'.'/'.$_POST['xlargeImage'];
					}

                    $value = $_POST['tag1'];
                    $_POST['tag'] ='';
                    if ($value){
                        foreach ($value as $v)
                        {
                            $tagname .= $v.',';
                    		$_POST['tag'] = substr($tagname,0,-1);
                        }
                    }

                    $_POST['id'] = $_POST['id'];
                    $_POST['shortDetail'] = stripslashes($_POST['shortDetail']);
            		$_POST['mediumDetail'] = stripslashes($_POST['mediumDetail']);
                    $_POST['longDetail'] = stripslashes($_POST['longDetail']);
                    $_POST['name'] = stripslashes($_POST['name']);
                    $_POST['mediumName'] = stripslashes($_POST['mediumName']);

                    $param = $_POST;
                    $gameService->updateGameMMBG($context, $result, $param);

                    header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=mmbg");
                }
                $this->smarty->assign('data',$data);
                $this->smarty->assign('gameTagArr', $gameTagArr);
                $this->smarty->assign('gamekinhdienArr',$gamekinhdienArr);
                $this->smarty->display('update-game.tpl');
            }
            elseif ($data == 'nsgt')
            {
            	$gamekinhdienArr = $gameCategoryService -> getGameCategoryByParentMobile($context, $result, 78);
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $game = $gameService->getGameByIDNSGT($context, $result, $id);
                $gameTagArr = $gameService->getFullGameTagNSGT($context, $result);
                $gameCategoryArr = $gameCategoryService->getFullGameCategoryNSGT($context, $result);

                $image = explode('/',$game['mediumImage']);
                
            	$kieugameKD = $image[2];
			 	if($image[1] == 'kinhdien')
		        {
		        	$img = $image[4];
		        }
		        else
		        {
		        	$img = $image[3];
		        }
            	$imagex = explode('/',$game['xlargeImage']);
				if($imagex[1] == 'kinhdien')
		        {
		        	$imgx = $image[4];
		        }
		        else
		        {
		        	$imgx = $image[3];
		        }
		        
                $loaiGame = $image[1];

                $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
                $this->smarty->assign('loaiGame', $loaiGame);
                $this->smarty->assign('img', $img);
                $this->smarty->assign('imgx', $imgx);
                $this->smarty->assign('game', $game);
                $this->smarty->assign('kieugameKD', $kieugameKD);
                $tag = $game['tag'];
                $mang = explode(',',$tag);

                $this->smarty->assign('mang', $mang);
                if(isset($_POST) && !empty($_POST))
                {
                	if ($_POST["loaigame"] == 3)
		            {
		                $folder = 'offline';
		            }
		            else if($_POST["loaigame"] == 78)
		            {
		            	 $folder = 'kinhdien';
		            }
		            else if($_POST["loaigame"] == 2 || $_POST["loaigame"] == 156)
		            {
		               $folder = 'online';
		            }
		        	else if($_POST["loaigame"] == 4)
		            {
		               $folder = 'webgame';
		            }
		            
                	if($_POST["kieugame"])
					{
						$kieugame = SEO::friendly_url_image($_POST["kieugame"]);
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'. $kieugame.'/thumbnail'.'/'.$_POST['image'];
		           		$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/small'.'/'.$_POST['image'];
		            	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/large'.'/'.$_POST['image'];
		            	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.$kieugame.'/xlarge'.'/'.$_POST['xlargeImage'];
					}
					else 
					{
						$_POST['smallImage'] = 'games'.'/'. $folder .'/'.'thumbnail'.'/'.$_POST['image'];
                    	$_POST['mediumImage'] = 'games'.'/'. $folder .'/'.'small'.'/'.$_POST['image'];
                    	$_POST['largeImage'] = 'games'.'/'. $folder .'/'.'large'.'/'.$_POST['image'];
                    	$_POST['xlargeImage'] = 'games'.'/'. $folder .'/'.'xlarge'.'/'.$_POST['xlargeImage'];
					}

                    $value = $_POST['tag1'];
                    $_POST['tag'] ='';
                    if ($value){
                        foreach ($value as $v)
                        {
                            $tagname .= $v.',';
                    		$_POST['tag'] = substr($tagname,0,-1);
                        }
                    }

                    $_POST['id'] = $_POST['id'];
                    $_POST['shortDetail'] = stripslashes($_POST['shortDetail']);
            		$_POST['mediumDetail'] = stripslashes($_POST['mediumDetail']);
                    $_POST['longDetail'] = stripslashes($_POST['longDetail']);
                    $_POST['name'] = stripslashes($_POST['name']);
                    $_POST['mediumName'] = stripslashes($_POST['mediumName']);

                    $param = $_POST;
                    $gameService->updateGameNSGT($context, $result, $param);

                    header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=nsgt");
                }
                $this->smarty->assign('data',$data);
                $this->smarty->assign('gameTagArr', $gameTagArr);
                $this->smarty->assign('gamekinhdienArr',$gamekinhdienArr);
                $this->smarty->display('update-game.tpl');
            }

            $this->smarty->assign('data',$data);
        }

    }

    public function deleteAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        if(isset($_GET['database']) && !empty($_GET['database']))
        {
            $data = $_GET['database'];
            if($data == 'tpg')
            {
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $gameService->deleteGameTPG($context, $result, $id);
                header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=tpg");
            }
            
        	if($data == 'mobile')
            {
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $gameService->deleteGameMobile($context, $result, $id);
                header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=mobile");
            }

            if($data == 'cgmb')
            {
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $gameService->deleteGameCGMB($context, $result, $id);
                header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=cgmb");
            }
            if($data == 'mmbg')
            {
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $gameService->deleteGameMMBG($context, $result, $id);
                header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=mmbg");
            }
            if($data == 'nsgt')
            {
                if(isset($_GET['id']) && !empty($_GET['id']))
                {
                    $id = $_GET['id'];
                }

                $gameService->deleteGameNSGT($context, $result, $id);
                header("location:".WEB_BASE_URL."/index.php?controller=game&action=list&database=nsgt");
            }

        }
    }
    
	public function filterAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
		$gameService -> updateEventByExpireDate($context, $result);
        header("location:".WEB_BASE_URL."/index.php?controller=game&action=list");

    }
    
	public function sendMailAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameService = new GameService();
        $gameCategoryService = new GameCategoryService();
        
        if(isset($_GET) && !empty($_GET))
        {
        	$id = $_GET['id'];
        }
        $getGamebyId = $gameService -> getGameByIDTPG($context, $result, $id);
    	$gameServiceArr = $gameService -> getSubsribeAll($context, $result);
    	$gameCategory = $gameCategoryService->getGameCategoryByIDTPG($context, $result, $getGamebyId['parent']);

		foreach ($gameServiceArr as $gameService)
		{
			$email=htmlentities(trim(stripslashes($gameService['email'])));
			$name=htmlentities(trim(stripslashes($gameService['name'])));
		    $to = $email;
		    $subject = 'Game '.$getGamebyId['name'].' - '.$getGamebyId['mediumName'];
		    $gameDetailLink = 'http://thanhphogame.com' . '/game-' . $gameCategory['unique_name'] . '/' . $getGamebyId['unique_name'] .'.html';
		    $body = '<a href="'.$gameDetailLink.'" title="'.$getGamebyId['name'].' - '.$getGamebyId['mediumName'].'">'.     
                         'Xem Chi Tiết Và Tải Game '.$getGamebyId['name'] . ' - ' . $getGamebyId['mediumName'].' Miễn Phí '.
                    '</a>
                    
            '.'Cám Ơn Bạn Đã Xem nhé. Chúc Bạn 1 Ngày Tốt Lành Nha!!! ';
		    
		    $headers = 'From: thanhphogame.com@gmail.com' . "\r\n" ;
		    $headers .= 'Reply-To: thanhphogame.com@gmail.com' . "\r\n";
		    mail($to, $subject, $body, $headers);
		    echo 'Đã gửi Thành Công : '.$gameService['email'];
		}
        header("location:".WEB_BASE_URL."/index.php?controller=game&action=list");

    }
   
}

?>